/*
 * CS 567 GUI - Semester Project - "DecisionLists"
 * Jason Frank
 * Spring 2011
 *
 * This File:  Makes a panel consisting of a rounded rectangle, possibly
 *             with a border or other qualities.
 *
 */

package Decision_Lists;

import java.awt.BasicStroke;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

/*
 */
public class RoundedRect_Panel extends JPanel {

  // Class Vars - many with default vals
  boolean hasBorder = true;
  Color borderColor = Color.BLACK;
  Color fg_color = Color.WHITE;
  int panelWidth;
  int panelHeight;



  // Constructor                                                // specify null for no border?
  public RoundedRect_Panel(int width, int height, Color fg_color, Color borderColor)  {

    panelWidth  = width;
    panelHeight = height;
    this.fg_color = fg_color;
    if(borderColor != null) {
      hasBorder = true;
      this.borderColor = borderColor;
    }
    else {
      hasBorder = false;
    }

  } ////// end default constructor //////


  @Override
  public void paintComponent(Graphics g) {

    ////// INTITIAL SETUP ////
    // Call the superclass paintComponent() to clear the clipping region (avoids ghosting).
    super.paintComponent(g);
    // make a deep copy of the Graphics object before modifying it
    Graphics myG = g.create();
    Graphics2D g2 = (Graphics2D) myG;
    // Trying to make things ANTI-ALIASED (doens't seem to work though)
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    // setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

    ////////////////////////////////////////////////////////////////////
    // Setup the OVERALL UNDERLYING SHAPE (rounded corner rectangle) ///
    g2.setStroke(new BasicStroke(3));

    g2.setColor(fg_color);
    g2.fillRoundRect(2, 2, getWidth() -4, getHeight() -4, 25, 25);
    
    if(hasBorder) {
      g2.setColor(borderColor);
      g2.drawRoundRect(2, 2, getWidth() -4, getHeight() -4, 25, 25);
        // Couldn't get the rounded corners to work with this...
        //    LineBorder roundedBorder = new LineBorder(borderColor, 1, true);
        //    System.out.println("Border will be drawn with rounded corners? " + roundedBorder.getRoundedCorners());
        //    setBorder(roundedBorder);
    }

    // clear up resources
    myG.dispose();

  } ///// end paintComponent() ///



  @Override
  public Dimension getMinimumSize()   { return getPreferredSize(); }
  @Override
  public Dimension getPreferredSize() { return new Dimension(panelWidth, panelHeight); }

} //////////////////// end class RoundedRect_Panel /////////////////////////////
